package com.lg.system.wxmaterial.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lg.config.BootLgConfig;
import com.lg.system.wxmaterial.dao.WxMaterialDao;
import com.lg.system.wxmaterial.entity.WxMaterial;
import com.lg.system.wxmaterial.service.WxMaterialService;
import com.lg.utils.FileUtil;
import com.lg.utils.R;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dl
 * @since 2018-04-08
 */
@Service
public class WxMaterialServiceImpl extends ServiceImpl<WxMaterialDao, WxMaterial> implements WxMaterialService {

	@Autowired
	BootLgConfig bootLgConfig;
	
	@Autowired
	WxMpService wxMpService;
	
	public boolean save( WxMaterial wxMaterial) {
		wxMaterial.setGmtCreate(new Date());
		return super.insert(wxMaterial);
	}

	@Override
	public boolean update(WxMaterial wxMaterial) {
		wxMaterial.setGmtModified(new Date());
		return super.updateById(wxMaterial);
	}
	
	
	@Override
	public boolean deleteById(Long id) {
		return super.deleteById(id);
	}

	@Override
	public boolean deleteByIds(List<Long> ids) {
		if(null==ids||ids.isEmpty()){
			return false;
		}
		return super.deleteBatchIds(ids);
	}

	@Override
	public boolean upload(MultipartFile files, String type,WxMaterial material) throws IOException, WxErrorException {//此处调用微信接口上传
		String finalPath[]=FileUtil.uploadFile(files.getBytes(), bootLgConfig.getUploadPath(), FileUtil.renameToUUID(files.getOriginalFilename()));
		WxMpMaterial wxMaterial = new WxMpMaterial();
		wxMaterial.setFile(new File(finalPath[0]));
		wxMaterial.setName(files.getOriginalFilename());
		//WxMaterial material=new WxMaterial();
		//判断上传的文件类型
		if("image".equals(type)){//上传图片
		 WxMpMaterialUploadResult res = wxMpService.getMaterialService().materialFileUpload(type, wxMaterial); 
		 material.setUrl(res.getUrl());
		 material.setMediaId(res.getMediaId());
		}else if("video".equals(type)||"voice".equals(type)){//上传视频
			 wxMaterial.setVideoIntroduction(material.getDesc());
			 wxMaterial.setVideoTitle(material.getTitle());
			 WxMpMaterialUploadResult res = wxMpService.getMaterialService().materialFileUpload(type, wxMaterial); 
			 material.setUrl(res.getUrl());
			 material.setMediaId(res.getMediaId());
		}
		 material.setFilename(files.getOriginalFilename());
		 material.setLocalUrl(bootLgConfig.getPathPattern()+finalPath[1]);
		 material.setGmtCreate(new Date());
		 material.setType(type);
		 material.setFilelength(files.getSize());
		 save(material);
		System.out.println("-----------------------success------------------------------");
		return true;
	}

	@Override
	public R mediaImgUpload(String path) throws WxErrorException {
		File file=new File(path);
		if(file.exists()){
			WxMediaImgUploadResult res=wxMpService.getMaterialService().mediaImgUpload(file);
			return R.ok(res.getUrl());
		}else{
			return R.error();
		}
		  
	}

	@Override
	public R mediaImgUpload(MultipartFile file) throws WxErrorException, IOException {
		String finalPath[]=FileUtil.uploadFile(file.getBytes(), bootLgConfig.getUploadPath(), FileUtil.renameToUUID(file.getOriginalFilename()));
		File fileLocal=new File(finalPath[0]);
		if(fileLocal.exists()){
			WxMediaImgUploadResult res=wxMpService.getMaterialService().mediaImgUpload(fileLocal);
			Map<String, Object> param=new HashMap<>();
			param.put("localurl",bootLgConfig.getPathPattern()+ finalPath[1]);
			param.put("wxurl", res.getUrl());
			return R.ok(param);
		}else{
			return R.error();
		}
	}
	
	

}
